// 路由守卫
import router from './router'
import { message } from 'ant-design-vue'
import NProgress from 'nprogress' // 浏览器进度条
import 'nprogress/nprogress.css' // 浏览器进度条样式
NProgress.configure({ showSpinner: false }) // 浏览器进度条配置
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  NProgress.start() // 开始加载浏览器进度条
  document.title = `人事档案查询系统-${to.meta.pageName}`
  const userData = JSON.parse(sessionStorage.getItem('USER_DATA'))
  // 判断有无登录
  if (userData) {
    if (to.path === '/login' || to.path === '/') {
      // 如果已经登录,跳转到首页
      next({ path: '/home' })
      NProgress.done()
    } else {
      next()
      // 判断有无权限
      if (userData.aU_IsSystem === 0) {
        if (to.path === '/auth') {
          message.warning('您不是管理员，没有权限访问')
          next({ path: '/' })
        } else {
          next()
        }
      } else {
        next()
      }
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束浏览器进度条
})
